package wec_wallet.ebo.com.activity.wallet;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import bassproject.ebo.com.ebobass.activity.MyBaseActivity;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import bassproject.ebo.com.ebobass.utils.Lg;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.common.Constant;
import wec_wallet.ebo.com.utils.Functions;

/**
 * 转账详情
 * Created by Administrator on 2018/3/30.
 */

public class TransferDetails extends MyBaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_transferDetails_coinName)
    TextView tvTransferDetailsCoinName;
    @BindView(R.id.tv_transferDetails_receivables)
    TextView tvTransferDetailsReceivables;
    @BindView(R.id.tv_transferDetails_coin)
    TextView tvTransferDetailsCoin;
    @BindView(R.id.tv_transferDetails_money)
    TextView tvTransferDetailsMoney;
    @BindView(R.id.tv_transferDetails_addressNo)
    TextView tvTransferDetailsAddressNo;
    @BindView(R.id.tv_transferDetails_address)
    TextView tvTransferDetailsAddress;
    @BindView(R.id.tv_transferDetails_time)
    TextView tvTransferDetailsTime;
    @BindView(R.id.tv_transferDetails_order)
    TextView tvTransferDetailsOrder;
    @BindView(R.id.tv_transferDetails_state)
    TextView tv_transferDetails_state;
    String id, type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer_details);
        ButterKnife.bind(this);
        initViews();
    }

    @Override
    public void initViews() {
        tvTitle.setText(getString(R.string.transferDetails));
        id = getIntent().getStringExtra("id");
        type = getIntent().getStringExtra("type");
//        getRecords();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getRecords();
    }

    @OnClick(R.id.actionbar_back)
    public void back() {
        finish();
    }

    @Override
    public void onClick(View view) {

    }

    /**
     * 获取转账记录列表
     */
    private void getRecords() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", I_Share.readAccountToken());
        map.put("uid", I_Share.readAccountUserid());
        map.put("orderid", id);
        map.put("type", type);
        Functions.post(this, Constant.URL_TRANSFERDETAILS, map, new
                ListenerRecords(), false, true);
    }

    private class ListenerRecords implements IHttpPostListener {
        @Override
        public void onTimeOut() {

        }

        @Override
        public void responseSuccess(String result) {
            if (Ipost.isTimeOut(result)) {
                return;
            }
            if (null != result) {
                try {
                    Log.e("-", "-re:" + result);
                    JSONObject resultObject = new JSONObject(result);
                    String status = resultObject.getString("error");
                    if (resultObject.has("is_auth") && resultObject.getString("is_auth").equals
                            ("0")) {
                        IToast.show(resultObject.getString("error_msg"));
                        Functions.relogin(TransferDetails.this);//登录过期
                        return;
                    }
                    if (resultObject.has("error_msg")) {
                        IToast.show(resultObject.getString("error_msg"));
                        return;
                    }
                    if (status.equals("0")) {
                        JSONObject data = new JSONObject(resultObject.getString("data"));
                        String str = getString(R.string.wallet_into);
                        if (type.equals("2")) {
                            str = getString(R.string.wallet_out);
                        }
                        tvTransferDetailsReceivables.setText(str);
                        tvTransferDetailsTime.setText(data.getString("created_at"));
                        tvTransferDetailsAddressNo.setText(data.getString("address_name"));
                        tvTransferDetailsAddress.setText(data.getString("address"));
                        tvTransferDetailsMoney.setText("≈" + data.getString
                                ("coin_cny_num") + " CNY");
                        tvTransferDetailsCoinName.setText(data.getString("wallet_name"));
                        tvTransferDetailsCoin.setText(data.getString("coin_number") + " WEC");
                        tvTransferDetailsOrder.setText(data.getString("order_num"));

                        String strStatus = data.getString("status");
                        String state = getString(R.string.transfer_record_out_error);
                        tv_transferDetails_state.setTextColor(getResources().getColor(R.color
                                .warning));
                        if (strStatus.equals("1")) {
                            tv_transferDetails_state.setTextColor(getResources().getColor(R.color
                                    .transfer_details));
                            state = getString(R.string.transfer_record_into_success);
                        }
                        if (strStatus.equals("2")) {
                            tv_transferDetails_state.setTextColor(getResources().getColor(R.color
                                    .yellow));
                            state = getString(R.string.transfer_record_into);
                        }
                        if (strStatus.equals("3")) {
                            tv_transferDetails_state.setTextColor(getResources().getColor(R.color
                                    .warning));
                            state = getString(R.string.transfer_record_into_error);
                        }
                        if (strStatus.equals("4")) {
                            tv_transferDetails_state.setTextColor(getResources().getColor(R.color
                                    .transfer_details));
                            state = getString(R.string.transfer_record_out_success);
                        }
                        if (strStatus.equals("5")) {
                            tv_transferDetails_state.setTextColor(getResources().getColor(R.color
                                    .yellow));
                            state = getString(R.string.transfer_record_out);
                        }
                        tv_transferDetails_state.setText(state);
                    }

                } catch (Exception e) {
                    // TODO: handle exception
                    Lg.d(getClass() + ",接口解析异常：\n" + e.toString());
                }
            } else {
                Lg.d("黑名单列表 result=null");

            }
        }
    }
}
